package itsm.isperp.module.controller.individuation;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.DataRequest;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.utils.TreeNodeUtils;
import itsm.isperp.framework.web.controller.TreeNodeController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.individuation.IddCategory;
import itsm.isperp.module.service.individuation.IddCategoryService;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 事件 分类管理控制器
 * 
 * @author lizx
 * @date 2013-9-6
 */
@Controller
@RequestMapping("individuation/category")
public class IddCategoryController extends TreeNodeController<IddCategory> {

	@Autowired
	IddCategoryService IddCategoryService;

	@Override
	public IddCategoryService getService() {
		return IddCategoryService;
	}

	@Override
	public String getPageTitle() {
		return "事件分类";
	}

	@RequestMapping("relateEgEdit")
	public ModelAndView roleUserEdit(String roleId) {
		ModelAndView mv = new ModelAndView("individuation/category/relateEgEdit");
		mv.addObject("roleId", roleId);
		System.out.print("ininin`=================");

		/*
		 * AppRole role = this.getService().findOne(roleId);
		 * mv.addObject("roleName", role.getName());
		 */
		return mv;

	}

	@RequestMapping("list")
	@ResponseBody
	public PageResponse<IddCategory> list(JqGridRequest request, HttpServletRequest req) {
		String term = req.getParameter("term");
		if (StringUtils.isNotEmpty(term)) {
			request.isPageable(false);
			request.setRows(15);
			request.setSearchString(" name LIKE '%" + term + "%'");
			return this.getService().findAll(request).toPage();

		} else {
			return this.getService().findAll(request).toPage();
		}

	}

	@RequestMapping("getFirstStepUsers")
	@ResponseBody
	public List<Map<String, Object>> getFirstStepUsers(JqGridRequest request, String categoryId, HttpServletRequest req) {
		String sql = "select description from app_dict where type_code='ICD_FIRSTSTEP_SUPPORTERS' AND code='" + categoryId + "'";
		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		String filter = jdbcTemplate.queryForObject(sql, String.class);
		String sql1 = "select name,fullname from (SELECT b.name,b.fullname,a.name role_name FROM v_app_user_role a INNER JOIN app_user b ON a.app_user_name=b.name) t where 1=1 " + filter;
		return jdbcTemplate.queryForList(sql1);
	}

	@RequestMapping("getIfExistsMainFormByParentId")
	@ResponseBody
	public ResultMessage getIfExistsMainFormByParentId(JqGridRequest request, String parentId, String id, HttpServletRequest req) {
		String sql = "select * from idd_category where  parent_id='" + parentId + "' and are_main_form=1 and id<>'" + id + "'";
		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		if (list.size() > 0)
			return ResultMessage.newInstance(true);
		else
			return ResultMessage.newInstance(false);

	}

	@Override
	@RequestMapping("getTree")
	@ResponseBody
	public List<IddCategory> getTree(String id, boolean asyncLoad, String parentId, boolean showRoot, HttpServletRequest req) {
		String[] columns = { "id", "name", "parent_id", "treenode_path",
				"leaf", "treenode_name_path", "open" };
		JqGridRequest request = new JqGridRequest();
		request.setSelectColumns(columns)
		.isPageable(false).setSort("order_num", Direction.ASC);
		if (StringUtils.isNotEmpty(parentId)) {

			IddCategory p = this.getService().findOne(parentId);
			request.addFilterRule("treenode_path", JqGridFilterRuleOp.bw, p.getTreenodePath());
		}
		String showMainForm=req.getParameter("showMainForm");
		if (StringUtils.isNotEmpty(showMainForm)) {

			request.setSearchString(" (leaf=0 or are_main_form=1)");
		}
		List<IddCategory> tree = this.getService().getRepository().findAllByRequest(request).getContent();

		Class<IddCategory> clazz = (Class<IddCategory>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

		return TreeNodeUtils.parseAndAddRootFormList(clazz, tree, showRoot);
	}
}
